package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.GIDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.QueryPageBeanT;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Group_Item;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass =CheckGroupService.class )
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private GIDao giDao;
    @Autowired
    private SetmealDao setmealDao;




    @Override
    public void add(CheckGroup checkGroup ,Integer [] checkitemIds) {

        checkGroupDao.add(checkGroup);
        int cGId =checkGroup.getId();
        if(checkitemIds!=null && checkitemIds.length>0) {
            for (int i = 0; i < checkitemIds.length; i++) {
                int ctId = checkitemIds[i];
                Group_Item group_item = new Group_Item();
                group_item.setCheckgroup_id(cGId);
                group_item.setCheckitem_id(ctId);
                giDao.add(group_item);


            }
        }
    }

    @Override
    public PageResult queryPage(QueryPageBeanT queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.queryPage(queryPageBean);
        long total = page.getTotal();
        List<CheckGroup> rows = page.getResult();
        return new PageResult(total,rows);

    }

    @Override
    public CheckGroup findone(int id) {
        CheckGroup checkGroup = checkGroupDao.findone(id);

        return checkGroup;
    }

    @Override
    public List<Integer> queryItemByid(int id) {
        List<Integer> integerList = giDao.queryItemByid(id);
        return integerList;
    }
    @Override
    public void update(CheckGroup checkGroup ,Integer [] checkitemIds) {
        int cGId =checkGroup.getId();
        giDao.deleteByid(checkGroup.getId());
        checkGroupDao.update(checkGroup);
        if(checkitemIds!=null && checkitemIds.length>0) {
            for (int i = 0; i < checkitemIds.length; i++) {
                int ctId = checkitemIds[i];
                Group_Item group_item = new Group_Item();
                group_item.setCheckgroup_id(cGId);
                group_item.setCheckitem_id(ctId);
                giDao.add(group_item);


            }
        }
    }

    @Override
    public Result delete(int id) {
        List<Setmeal> setmealList = setmealDao.query(id);
        if(setmealList.size()>0){
            return new Result(false,"此检查组被使用，无法被删除");
        }else{
            try {
                giDao.deleteByid(id);
                checkGroupDao.delete(id);

            } catch (Exception e) {
                e.printStackTrace();
                return  new Result(false,"此检查组无法被删除");
            }
            return new Result(true,"删除成功");
        }

    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();

    }


}
